package com.cabrida.livestar.util;

import java.io.File;

import android.os.Environment;
import android.os.StatFs;


public class SDCardUtils {  
	private SDCardUtils(){  
		/* cannot be instantiated */  
		throw new UnsupportedOperationException("cannot be instantiated");  
	}  

	/** 
	 * Check SDCard availability
	 *  
	 * @return if available,return true;if not available,return false
	 */  
	public static boolean isSDCardEnable(){  
		return Environment.getExternalStorageState().equals(  
				Environment.MEDIA_MOUNTED);  
	}  

	/** 
	 * Get SDCard path 
	 *  
	 * @return SDCard path
	 */  
	public static String getSDCardPath(){  
		return Environment.getExternalStorageDirectory().getAbsolutePath()  
				+ File.separator;  
	}  

	/** 
	 * Get the SDcard remaining capacity (unit:byte) 
	 *  
	 * @return if true,return remaining capacity;if false,return 0
	 */  
	@SuppressWarnings("deprecation")
	public static long getSDCardAllSize(){  
		if (isSDCardEnable()){  
			StatFs stat = new StatFs(getSDCardPath());  
			// Get the number of free blocks of data  
			long availableBlocks = (long) stat.getAvailableBlocks() - 4;  
			// Get the size of the access to a single block of data（byte）  
			long freeBlocks = stat.getAvailableBlocks();  
			return freeBlocks * availableBlocks;  
		}  
		return 0;  
	}  

	/** 
	 * Get the specified path space remaining bytes available capacity (unit:byte) 
	 *  
	 * @param filePath 
	 * @return SDCard capacity bytes available space, internal storage space available 
	 */  
	@SuppressWarnings("deprecation")
	public static long getFreeBytes(String filePath){  
		// If it is the path of the SDCard,then get the available capacity for SDCard  
		if(filePath.startsWith(getSDCardPath())){  
			filePath = getSDCardPath();  
		}else{
			// If it is the path of the internal storage，then get the available capacity for memory storage  
			filePath = Environment.getDataDirectory().getAbsolutePath();  
		}  
		StatFs stat = new StatFs(filePath);  
		long availableBlocks = (long) stat.getAvailableBlocks() - 4;  
		return stat.getBlockSize() * availableBlocks;  
	}  

	/** 
	 * Get the system storage path 
	 *  
	 * @return system storage path
	 */  
	public static String getRootDirectoryPath(){  
		return Environment.getRootDirectory().getAbsolutePath();  
	}  

}  
